package com.bibrApp.difu.actvity.jiedai_manager_mvp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.order_detail_mvp.OrderDetailActivity;
import com.bibrApp.difu.actvity.others.HelpActivity;
import com.bibrApp.difu.actvity.wait_delivery_mvp.WaitDeliveryActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.fragment.jieru_mvp.JiedaiManagerAdapter;
import com.bibrApp.difu.fragment.jieru_mvp.JiedaiManagerBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class JiedaiManagerActivity extends BaseActivity<JiedaiManagerPresenter> implements IJiedaiManagerView {


    @BindView(R.id.tv_jiechu)
    TextView tvLeft;
    @BindView(R.id.tv_jieru)
    TextView tvRight;
    //    @BindView(R.id.tab_list)
//    RecyclerView tabList;
//
//    @BindView(R.id.viewpager)
//    HomeViewPager viewPager;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    String[] tabs1 = {"当前借出", "历史借出", "当前发布", "当前借入", "历史借入", "补仓记录"};

    int type = 0;
    int pageNum = 1;
    int pageSize = 10;

    List<String> tabs = new ArrayList<>();
    Map<String, Fragment> fragments = new HashMap<>();
    private TabAdapter tabAdapter;
    private JiedaiManagerAdapter adapter;

    @Override
    protected void initPresenter() {
        presenter = new JiedaiManagerPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jiedai_manager;
    }


    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void onDataResult(List<JiedaiManagerBean> list, boolean hasNextPage) {
        if (pageNum == 1 && (list == null || list.size() == 0) && adapter.getEmptyViewCount() == 0) {
            adapter.setEmptyView(View.inflate(this, R.layout.empty_order, null));
            return;
        }
        adapter.addData(list);
        if (!hasNextPage) {
            adapter.loadMoreEnd();
        } else {
            adapter.loadMoreComplete();
        }
    }


    @Override
    public void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new JiedaiManagerAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(() -> {
            pageNum++;
            presenter.getDatas(type, pageNum, pageSize);
        }, recyclerView);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(JiedaiManagerActivity.this, OrderDetailActivity.class);
            intent.putExtra("item", adapter.getItem(position));
            startActivity(intent);
        });

//        tabList.setLayoutManager(new GridLayoutManager(this, 4));
//        tabAdapter = new TabAdapter();
//        tabList.setAdapter(tabAdapter);
//        tabAdapter.setOnItemClickListener((adapter, view, position) -> {
//            tabAdapter.changeSelect(position);
//            if (tabs.size() == 2) {
//                viewPager.setCurrentItem(position, false);
//            } else {
//                viewPager.setCurrentItem(position + 2, false);
//            }
//        });
//        viewPager.setOffscreenPageLimit(1);
//        viewPager.setNoScroll(true);
//        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
//
//            @NonNull
//            @Override
//            public Fragment getItem(int position) {
//                Fragment fragment = fragments.get(tabs1[position]);
//                if (fragment != null) {
//                    return fragment;
//                }
//                if (position == 0) {
//                    fragment = new JieruFragment(0, "1");
//                } else if (position == 1) {
//                    fragment = new JieruFragment(0, "2");
//                } else if (position == 2) {
//                    fragment = new JieruFragment(1, "0");
//                } else if (position == 3) {
//                    fragment = new JieruFragment(1, "1");
//                } else if (position == 4) {
//                    fragment = new JieruFragment(1, "2");
//                } else {
//                    fragment = new BucangRecordFragment();
//                }
//                fragments.put(tabs1[position], fragment);
//                return fragment;
//            }
//
//            @Override
//            public int getCount() {
//                return tabs1.length;
//            }
//        });


        leftClick();

    }

    @OnClick(R.id.bt_back)
    public void backClick() {
        finish();
    }

    @OnClick(R.id.bt_help)
    public void helpClick() {
        startActivity(new Intent(this, HelpActivity.class));
    }

    @OnClick(R.id.tv_jiechu)
    public void leftClick() {
        tvLeft.setBackgroundResource(R.drawable.bg_jiedai_tab_left);
        tvRight.setBackgroundResource(R.drawable.bg_jiedai_tab_right);
        tvLeft.setTextColor(getResources().getColor(R.color.white));
        tvRight.setTextColor(getResources().getColor(R.color.blue_e4));

        adapter.setNewData(new ArrayList<>());
        type = 0;
        pageNum = 1;
        presenter.getDatas(type, pageNum, pageSize);
//        tabs.clear();
//        tabs.add("当前借出");
//        tabs.add("历史借出");
//        tabAdapter.setDatas(tabs, 0);
//        viewPager.setCurrentItem(0,false);
    }

    @OnClick(R.id.tv_jieru)
    public void rightClick() {
        tvLeft.setBackgroundResource(R.drawable.bg_jiedai_tab_left2);
        tvRight.setBackgroundResource(R.drawable.bg_jiedai_tab_right2);
        tvLeft.setTextColor(getResources().getColor(R.color.blue_e4));
        tvRight.setTextColor(getResources().getColor(R.color.white));
        adapter.setNewData(new ArrayList<>());
        type = 1;
        pageNum = 1;
        presenter.getDatas(type, pageNum, pageSize);
//        tabs.clear();
//        tabs.add("当前发布");
//        tabs.add("当前借入");
//        tabs.add("历史借入");
//        tabs.add("补仓记录");
//        tabAdapter.setDatas(tabs, 0);
//        viewPager.setCurrentItem(2,false);
    }
}
